793. 阶乘函数后 K 个零
为保证权益,题目请参考 793. 阶乘函数后 K 个零(From LeetCode).
解决方案1
Python
python
# 793. 阶乘函数后 K 个零
# https://leetcode.cn/problems/preimage-size-of-factorial-zeroes-function/
from functools import lru_cache
from typing import Callable, Sequence, Tuple
# 逻辑正确,但是超时,划去
# class Solution:
# def preimageSizeFZF(self, k: int) -> int:
# d = dict()
# def get_2_5(n: int) -> Tuple[int, int]:
# if n == 0:
# return 0, 0
# if n in d:
# return d[n]
# if n % 2 == 0:
# n2, n5 = get_2_5(n // 2)
# d[n] = n2 + 1, n5
# return d[n]
# if n % 5 == 0:
# n2, n5 = get_2_5(n // 5)
# d[n] = n2, n5 + 1
# return d[n]
# d[n] = 0,0
# return d[n]
# ans = 0
# n2 = 0
# n5 = 0
# i = 0
# while True:
# n2_, n5_ = get_2_5(i)
# print(i,n2_,n5_)
# n2 += n2_
# n5 += n5_
# if min(n2, n5) == k:
# ans += 1
# elif min(n2, n5) > k:
# break
# i += 1
# return ans
import bisect
class Solution:
def preimageSizeFZF(self, k: int) -> int:
@lru_cache()
def getz(n: int) -> int:
ans = 0
while n != 0:
n = n // 5
ans += n
return ans
def my_bisect_left(nums: Sequence[int], k: int) -> int:
l = 0
r = len(nums) - 1
while l <= r:
m = (l + r) // 2
t = getz(nums[m])
if t > k:
r = m - 1
elif t == k:
r = m - 1
elif t < k:
l = m + 1
if l >= len(nums):
return len(nums)
return l
def mk(k: int) -> int:
return my_bisect_left(range(5 * k), k)
return mk(k + 1) - mk(k)
if __name__ == "__main__":
so = Solution()
print(so.preimageSizeFZF(0))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95